﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text.RegularExpressions;
using Entity.IRepository.Project;
using Entity.ProjectEnt;
using ZFine.Code;
using ZFine.Data;
using ZFine.Domain.Entity;
using ZFine.Domain.IRepository.SystemManage;

namespace Logic.Repository.Project
{
    public class PositionRecordRepository :RepositoryBase<PositionRecordEntity>,IPositionRecordRepository
    {
        public PositionRecordEntity FindEntityinclude(Expression<Func<PositionRecordEntity, bool>> predicate)
        {
           // .Include(f => f.BanciUser)
            return dbcontext.Set<PositionRecordEntity>().Include(f => f.CreateAdmin)
                .Include(f => f.DiaochezhangUser).Include(f => f.DispatcherUser).Include(f => f.FusijiUser)
                .Include(f => f.HuoyunyunaUser2).Include(f => f.HuoyunyunaUser1).Include(f => f.SijiUser)
                .Include(f => f.TougouyuanUser).Include(f => f.ZhongzhuanyuanUser).Include(f => f.daokouyuanUser1)
                .Include(f => f.daokouyuanUser2).Where(predicate).FirstOrDefault();
        }

        /// <summary>
        /// 职位增加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int AddPostition(PositionRecordEntity entity)
        {
            if (entity.CreateAdmin != null)
            {
                dbcontext.Entry<UserEntity>(entity.CreateAdmin).State = EntityState.Unchanged;
                dbcontext.Entry(entity).Reference(f => f.CreateAdmin).CurrentValue = entity.CreateAdmin;
            }
            if (entity.DiaochezhangUser != null)
            {
                dbcontext.Entry<UserEntity>(entity.DiaochezhangUser).State = EntityState.Unchanged;
                dbcontext.Entry(entity).Reference(f => f.DiaochezhangUser).CurrentValue = entity.DiaochezhangUser;
            }
            if (entity.TougouyuanUser != null)
            {
                dbcontext.Entry<UserEntity>(entity.TougouyuanUser).State = EntityState.Unchanged;
                dbcontext.Entry(entity).Reference(f => f.TougouyuanUser).CurrentValue = entity.TougouyuanUser;
            }
            if (entity.ZhongzhuanyuanUser != null)
            {
                dbcontext.Entry<UserEntity>(entity.ZhongzhuanyuanUser).State = EntityState.Unchanged;
                dbcontext.Entry(entity).Reference(f => f.ZhongzhuanyuanUser).CurrentValue = entity.ZhongzhuanyuanUser;
            }
            if (entity.SijiUser != null)
            {
                dbcontext.Entry<UserEntity>(entity.SijiUser).State = EntityState.Unchanged;
                dbcontext.Entry(entity).Reference(f => f.SijiUser).CurrentValue = entity.SijiUser;
            }
            if (entity.FusijiUser != null)
            {
                dbcontext.Entry<UserEntity>(entity.FusijiUser).State = EntityState.Unchanged;
                dbcontext.Entry(entity).Reference(f => f.FusijiUser).CurrentValue = entity.FusijiUser;
            }
            if (entity.HuoyunyunaUser1 != null)
            {
                dbcontext.Entry<UserEntity>(entity.HuoyunyunaUser1).State = EntityState.Unchanged;
                dbcontext.Entry(entity).Reference(f => f.HuoyunyunaUser1).CurrentValue = entity.HuoyunyunaUser1;
            }
            if (entity.HuoyunyunaUser2 != null)
            {
                dbcontext.Entry<UserEntity>(entity.HuoyunyunaUser2).State = EntityState.Unchanged;
                dbcontext.Entry(entity).Reference(f => f.HuoyunyunaUser2).CurrentValue = entity.HuoyunyunaUser2;
            }
            if (entity.daokouyuanUser1 != null)
            {
                dbcontext.Entry<UserEntity>(entity.daokouyuanUser1).State = EntityState.Unchanged;
                dbcontext.Entry(entity).Reference(f => f.daokouyuanUser1).CurrentValue = entity.daokouyuanUser1;
            }
            if (entity.daokouyuanUser2 != null)
            {
                dbcontext.Entry<UserEntity>(entity.daokouyuanUser2).State = EntityState.Unchanged;
                dbcontext.Entry(entity).Reference(f => f.daokouyuanUser2).CurrentValue = entity.daokouyuanUser2;
            }
            if (entity.DispatcherUser != null)//特殊处理（创建人和调度员是同一个人）
            {
                if (entity.CreateAdmin != null && entity.CreateAdmin.F_Id == entity.DispatcherUser.F_Id)
                {
                    dbcontext.Entry(entity).Reference(f => f.DispatcherUser).CurrentValue = entity.CreateAdmin;
                    dbcontext.Entry<UserEntity>(entity.DispatcherUser).State = EntityState.Unchanged;
                }
                else
                {
                    //dbcontext.Entry<UserEntity>(entity.DispatcherUser).State = EntityState.Modified;   删除用户问题修改
                    dbcontext.Entry<UserEntity>(entity.DispatcherUser).State = EntityState.Unchanged;
                    dbcontext.Entry(entity).Reference(f => f.DispatcherUser).CurrentValue = entity.DispatcherUser;
                }
            }
            dbcontext.Entry<PositionRecordEntity>(entity).State = EntityState.Added;
            return dbcontext.SaveChanges();
        }
        

        public List<PositionRecordEntity> FindList(Pagination pagination)
        {
            bool isAsc = pagination.sord.ToLower() == "asc" ? true : false;
            string[] _order = pagination.sidx.Split(',');
            MethodCallExpression resultExp = null;
            var tempData = dbcontext.Set<PositionRecordEntity>().Include(f => f.CreateAdmin)
                .Include(f => f.DiaochezhangUser).Include(f => f.DispatcherUser).Include(f => f.FusijiUser)
                .Include(f => f.HuoyunyunaUser2).Include(f => f.HuoyunyunaUser1).Include(f => f.SijiUser)
                .Include(f => f.TougouyuanUser).Include(f => f.ZhongzhuanyuanUser).Include(f => f.daokouyuanUser1)
                .Include(f => f.daokouyuanUser2).AsQueryable();
            foreach (string item in _order)
            {
                string _orderPart = item;
                _orderPart = Regex.Replace(_orderPart, @"\s+", " ");
                string[] _orderArry = _orderPart.Split(' ');
                string _orderField = _orderArry[0];
                bool sort = isAsc;
                if (_orderArry.Length == 2)
                {
                    isAsc = _orderArry[1].ToUpper() == "ASC" ? true : false;
                }
                var parameter = Expression.Parameter(typeof(PositionRecordEntity), "t");
                var property = typeof(PositionRecordEntity).GetProperty(_orderField);
                var propertyAccess = Expression.MakeMemberAccess(parameter, property);
                var orderByExp = Expression.Lambda(propertyAccess, parameter);
                resultExp = Expression.Call(typeof(Queryable), isAsc ? "OrderBy" : "OrderByDescending", new Type[] { typeof(PositionRecordEntity), property.PropertyType }, tempData.Expression, Expression.Quote(orderByExp));
            }
            tempData = tempData.Provider.CreateQuery<PositionRecordEntity>(resultExp);
            pagination.records = tempData.Count();
            tempData = tempData.Skip<PositionRecordEntity>(pagination.rows * (pagination.page - 1)).Take<PositionRecordEntity>(pagination.rows).AsQueryable();
            return tempData.ToList();
        }

        public List<PositionRecordEntity> FindList(Expression<Func<PositionRecordEntity, bool>> predicate, Pagination pagination)
        {
            bool isAsc = pagination.sord.ToLower() == "asc" ? true : false;
            string[] _order = pagination.sidx.Split(',');
            MethodCallExpression resultExp = null;
            var tempData = dbcontext.Set<PositionRecordEntity>().Include(f => f.CreateAdmin)
                .Include(f => f.DiaochezhangUser).Include(f => f.DispatcherUser).Include(f => f.FusijiUser)
                .Include(f => f.HuoyunyunaUser2).Include(f => f.HuoyunyunaUser1).Include(f => f.SijiUser)
                .Include(f => f.TougouyuanUser).Include(f => f.ZhongzhuanyuanUser).Include(f => f.daokouyuanUser1)
                .Include(f => f.daokouyuanUser2).Where(predicate);
            foreach (string item in _order)
            {
                string _orderPart = item;
                _orderPart = Regex.Replace(_orderPart, @"\s+", " ");
                string[] _orderArry = _orderPart.Split(' ');
                string _orderField = _orderArry[0];
                bool sort = isAsc;
                if (_orderArry.Length == 2)
                {
                    isAsc = _orderArry[1].ToUpper() == "ASC" ? true : false;
                }
                var parameter = Expression.Parameter(typeof(PositionRecordEntity), "t");
                var property = typeof(PositionRecordEntity).GetProperty(_orderField);
                var propertyAccess = Expression.MakeMemberAccess(parameter, property);
                var orderByExp = Expression.Lambda(propertyAccess, parameter);
                resultExp = Expression.Call(typeof(Queryable), isAsc ? "OrderBy" : "OrderByDescending", new Type[] { typeof(PositionRecordEntity), property.PropertyType }, tempData.Expression, Expression.Quote(orderByExp));
            }
            tempData = tempData.Provider.CreateQuery<PositionRecordEntity>(resultExp);
            pagination.records = tempData.Count();
            tempData = tempData.Skip<PositionRecordEntity>(pagination.rows * (pagination.page - 1)).Take<PositionRecordEntity>(pagination.rows).AsQueryable();
            return tempData.ToList();
        }
    }
}
